package sk.tuke.client.view;

import sk.tuke.client.interfaces.LayoutFactory;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.form.ValuesManager;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class ProfileView extends HLayout {
	
	final ValuesManager doctorValuesManager = new ValuesManager();
	final ValuesManager insuranceValuesManager = new ValuesManager();
	final ValuesManager departmentValuesManager = new ValuesManager();
	
	//Doctor's personal number
	private static final String PERSONAL_NUMBER = "123456";
	
    public ProfileView() {
    	
    	initLayout();
    	
    	RestDataSource datasource = new RestDataSource();  
        datasource.setDataURL("http://localhost:8080/healthCard/usermanagement/getdoctors");  
        datasource.setRecordXPath("GetDoctorOutput/doctors");  
        datasource.setCallbackParam("GetDoctorOutput");
        datasource.setDataFormat(DSDataFormat.JSON);
        
        RestDataSource insuranceDatasource = new RestDataSource();  
        insuranceDatasource.setDataURL("http://localhost:8080/healthCard/businessmanagement/getinsurance");  
        insuranceDatasource.setRecordXPath("GetInsuranceOutput/insurance");  
        insuranceDatasource.setCallbackParam("GetInsuranceOutput");
        insuranceDatasource.setDataFormat(DSDataFormat.JSON);
        
        RestDataSource departmentDatasource = new RestDataSource();  
        departmentDatasource.setDataURL("http://localhost:8080/healthCard/businessmanagement/getdepartment");  
        departmentDatasource.setRecordXPath("GetDepartmentOutput/department");  
        departmentDatasource.setCallbackParam("GetDepartmentOutput");
        departmentDatasource.setDataFormat(DSDataFormat.JSON);
        
        doctorValuesManager.setDataSource(datasource);
        insuranceValuesManager.setDataSource(insuranceDatasource);
        departmentValuesManager.setDataSource(departmentDatasource);
        
        Criteria criteria = new Criteria("number", PERSONAL_NUMBER);
        doctorValuesManager.fetchData(criteria, new DSCallback() {
			public void execute(DSResponse response, Object rawData, DSRequest request) {
		    	String html = "<span style='font-size: 14px'><b>" +
		    			doctorValuesManager.getValueAsString("salutation") + " " +
		    			doctorValuesManager.getValueAsString("firstname") + " " +
		    			doctorValuesManager.getValueAsString("middlename") + " " +
		    			doctorValuesManager.getValueAsString("lastname") + "</b><br/>" +
		    			"</span><span style='font-size: 12px'><b>Personal ID:</b> " + doctorValuesManager.getValueAsString("number") + "<br/>" +
		    			"<b>Phone:</b> " + doctorValuesManager.getValueAsString("phone/phoneNumber") + "<br/>"+
		    			"<b>E-Mail:</b> " + doctorValuesManager.getValueAsString("email") + "</span>";
				personalInfoHTMLPane.setContents(html);
				personalInfoHTMLPane.redraw();
				
				html = "<span style='font-size: 13px'>" + doctorValuesManager.getValueAsString("address/street") + " " +
        		doctorValuesManager.getValueAsString("address/streetNum") + "<br/>" +
        		doctorValuesManager.getValueAsString("address/zip") + " " +
        		doctorValuesManager.getValueAsString("address/city") + "<br/>" +
        		doctorValuesManager.getValueAsString("address/country") + "</span>";
				addressHTMLPane.setContents(html);
				addressHTMLPane.redraw();
		
        
		        Criteria criteria = new Criteria("number", doctorValuesManager.getValueAsString("insuranceNumber"));
		        insuranceValuesManager.fetchData(criteria, new DSCallback() {
					public void execute(DSResponse response, Object rawData, DSRequest request) {
						String html = "<span style='font-size: 12px'><b>" +
				    			insuranceValuesManager.getValue("name").toString() + "</b><br/>" +
				    			insuranceValuesManager.getValue("street").toString() + " " +
				    			insuranceValuesManager.getValue("streetNum").toString() + "<br/>" +
				    			insuranceValuesManager.getValue("zip").toString() + " " +
				    			insuranceValuesManager.getValue("city").toString() + "<br/>" +
				    			insuranceValuesManager.getValue("state").toString() +
				    			"<br/><br/><b>Insurance number:</b> " + insuranceValuesManager.getValue("number").toString() + "<br/>" +
				    			"<b>Contact person:</b> " + insuranceValuesManager.getValue("contactPerson").toString() + "<br/>" +
				    			"<b>Phone:</b> " + insuranceValuesManager.getValue("phone").toString() + "<br/>" +
				    			"<b>E-Mail:</b> " + insuranceValuesManager.getValue("email").toString() + "</span>";
						insuranceHTMLPane.setContents(html);
						insuranceHTMLPane.redraw();
					}
				});
		        
		        criteria = new Criteria("number", doctorValuesManager.getValueAsString("departmentNumber"));
		        departmentValuesManager.fetchData(criteria, new DSCallback() {
					public void execute(DSResponse response, Object rawData, DSRequest request) {
						String html = "<span style='font-size: 12px'><b>" +
								departmentValuesManager.getValue("name").toString() + "</b><br/>" +
								departmentValuesManager.getValue("description").toString() + "</span>";
						departmentHTMLPane.setContents(html);
						departmentHTMLPane.redraw();
					}
				});
        
			}
		});
    }
       
    private void initLayout(){
    	
    	String html = "No data";
    	personalInfoHTMLPane.setWidth100();
    	personalInfoHTMLPane.setHeight100();
    	personalInfoHTMLPane.setPadding(10);
    	personalInfoHTMLPane.setOverflow(Overflow.VISIBLE);
    	personalInfoHTMLPane.setContents(html);
    	personalInfoSectionStack = new SectionStack();
        personalInfoSectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
        personalInfoSectionStack.setWidth100();
        personalInfoSectionStack.setAutoHeight();
        personalInfoSectionStack.setOverflow(Overflow.VISIBLE);
        personalInfoSection.addItem(personalInfoHTMLPane);
        personalInfoSection.setExpanded(true);
        personalInfoSectionStack.addSection(personalInfoSection);
        
        insuranceHTMLPane.setWidth100();
    	insuranceHTMLPane.setHeight100();
    	insuranceHTMLPane.setPadding(10);
    	insuranceHTMLPane.setOverflow(Overflow.VISIBLE);
    	insuranceHTMLPane.setContents(html);
        insuranceSectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
        insuranceSectionStack.setWidth100();
        insuranceSectionStack.setAutoHeight();
        insuranceSectionStack.setOverflow(Overflow.VISIBLE);
        insuranceSection.addItem(insuranceHTMLPane);
        insuranceSection.setExpanded(true);
        insuranceSectionStack.addSection(insuranceSection);
        
    	addressHTMLPane.setWidth100();
    	addressHTMLPane.setHeight100();
    	addressHTMLPane.setPadding(10);
    	addressHTMLPane.setOverflow(Overflow.VISIBLE);
    	addressHTMLPane.setContents(html);
        addressSectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
        addressSectionStack.setWidth100();
        addressSectionStack.setAutoHeight();
        addressSectionStack.setOverflow(Overflow.VISIBLE);
        addressSection.addItem(addressHTMLPane);
        addressSection.setExpanded(true);
        addressSectionStack.addSection(addressSection);
        
		departmentHTMLPane.setWidth100();
		departmentHTMLPane.setHeight100();
		departmentHTMLPane.setPadding(10);
		departmentHTMLPane.setOverflow(Overflow.VISIBLE);
		departmentHTMLPane.setContents(html);
		departmentSectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		departmentSectionStack.setWidth100();
		departmentSectionStack.setAutoHeight();
		departmentSectionStack.setOverflow(Overflow.VISIBLE);
		departmentSection.addItem(departmentHTMLPane);
		departmentSection.setExpanded(true);
		departmentSectionStack.addSection(departmentSection);
        
        leftCol.setMembersMargin(30);
        leftCol.setMargin(30);
        leftCol.addMember(personalInfoSectionStack);
        leftCol.addMember(insuranceSectionStack);

        rightCol.setMembersMargin(30);
        rightCol.setMargin(30);
        rightCol.addMember(addressSectionStack);
        rightCol.addMember(departmentSectionStack);
        
        this.setWidth100();
        this.addMember(leftCol);
        this.addMember(rightCol);
        this.draw(); 
    }
    
    VLayout leftCol = new VLayout();
    VLayout rightCol  = new VLayout();
    SectionStack personalInfoSectionStack = new SectionStack();
    SectionStack insuranceSectionStack = new SectionStack();
    SectionStack addressSectionStack = new SectionStack();
    SectionStack departmentSectionStack = new SectionStack();
    SectionStackSection personalInfoSection = new SectionStackSection("Personal info");
    SectionStackSection insuranceSection = new SectionStackSection("Insurance");
    SectionStackSection addressSection = new SectionStackSection("Address");
    SectionStackSection departmentSection = new SectionStackSection("Department");
    HTMLPane personalInfoHTMLPane = new HTMLPane();
    HTMLPane insuranceHTMLPane = new HTMLPane();
    HTMLPane addressHTMLPane = new HTMLPane();
    HTMLPane departmentHTMLPane = new HTMLPane();
   
	public static class Factory implements LayoutFactory {

		public Canvas create() {
			return new ProfileView();
		}

	}
}
